<?php
include('includes/connection_settings.php');

if (!isset($_COOKIE['username'])) {
 echo("Log in first!");
 exit;
}

echo "<h1>Swich dictionaries</h1>";

$allDictionaries = mysql_query("SELECT dictionary FROM choices ORDER BY dictionary ASC");
if (!isset($allDictionaries)) {
 echo "<h1>Can't choose data from the table!</h1><p>" . mysql_error($connection) . "</p>";
 exit;
}

if (mysql_result($allDictionaries, 0, 0) == "") {
 echo "<p>No dictionaries available! Please, <span class='likeLink' onClick='showAddWordsMenu()'>create your own dictionaries</span></p>";
}

else {
 echo "<table>";

 //Seeks if dictionary already selected and echos checkboxes
 for ($i = 0; $i < mysql_num_rows($allDictionaries); $i++) {
  $currentDictionary = mysql_result($allDictionaries, $i, 0);

  $isItChoosenDictionary = mysql_query("SELECT {$_COOKIE['username']} FROM choices WHERE dictionary = '{$currentDictionary}'");
  $isItChoosenDictionary = mysql_result($isItChoosenDictionary, 0, 0);

  if ($isItChoosenDictionary) {
   echo "<tr><td><input type='checkbox' checked='checked' onClick='setDictionary(\"" . mysql_result($allDictionaries, $i, 0) . "\", this.checked)' id='" . mysql_result($allDictionaries, $i, 0) . "' /></td><td><p>" . mysql_result($allDictionaries, $i, 0) . "</p></td></tr>";
  }

  else {
   echo "<tr><td><input type='checkbox' onClick='setDictionary(\"" . mysql_result($allDictionaries, $i, 0) . "\", this.checked)' id='" . mysql_result($allDictionaries, $i, 0) . "' /></td><td><p>" . mysql_result($allDictionaries, $i, 0) . "</p></td></tr>";
  }
 }

 echo "</table>";
}

?>